perm filename ARITH.PAS[AL,HE]1 blob
sn#661636 filedate 1982-09-27 generic text, type C, neo UTF8
COMMENT ⊗ VALID 00007 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002 (*$E+ Arithmetic routines used by AL *)
C00004 00003 (* external routines *)
C00005 00004 (* trig & scalar functions: vdot, vmagn *)
C00008 00005 (* vector functions: vmake, svmul, vsdiv, vadd, vsub, unitv, crossv, tvmul *)
C00012 00006 (* trans extraction routines: tpos, torient, taxis, tmagn *)
C00017 00007 (* trans functions: tmake, tvadd, tvsub, ttmul, tinvrt, vsaxwr, constr, vmkfrc *)
C00025 ENDMK
C⊗;
(*$E+ Arithmetic routines used by AL *)
program arith;
const pi = 3.1415926535; rad = 0.0174532925;
type scalar = real;
vectorval = array [1..3] of real;
vector = record refcnt: integer; val: vectorval end;
vectorp = ↑vector;
transval = array [1..3,1..4] of real;
trans = record refcnt: integer; val: transval end;
transp = ↑trans;
cstring = packed array [0..9] of ascii;
c5str = packed array [0..4] of ascii;
c20str = packed array [0..19] of ascii;
function newVector: vectorp; extern;
procedure relVector(v: vectorp); extern;
function newTrans: transp; extern;
procedure relTrans(t: transp); extern;
(* external routines *)
procedure ppLine; extern; (* from EDIT.PAS *)
procedure ppOutNow; extern;
procedure ppChar(ch: ascii); extern;
procedure pp5(ch: c5str; length: integer); extern;
procedure pp10(ch: cstring; length: integer); extern;
procedure pp10L(ch: cstring; length: integer);extern;
procedure pp20(ch: c20str; length: integer); extern;
procedure pp20L(ch: c20str; length: integer); extern;
procedure ppReal(r: real); extern;
(* trig & scalar functions: vdot, vmagn *)
function sind(d: real): real; begin sind := sin(rad*d) end;
function cosd(d: real): real; begin cosd := cos(rad*d) end;
function tand(d: real): real; begin tand := sin(rad*d)/cos(rad*d) end;
function asin(x: real): real;
begin
if x = 1.0 then asin := 90.0
else asin := arctan(x/sqrt(1.0-x*x))/rad;
end;
function acos(x: real): real;
var s: real;
begin
if x = 0.0 then acos := 90.0
else
begin
s := arctan(sqrt(1.0-x*x)/x)/rad;
if x < 0 then acos := 180.0 + s else acos := s;
end;
end;
function atan2(y,x: real): real; (* 4-quadrant arctan(y/x) *)
var ans: real;
begin
if x = 0.0 then
begin
if y = 0.0 then ans := 0.0 (* Actually indeterminate, but ... *)
else if y > 0.0 then ans := 90.0
else ans := -90.0;
end
else
begin
ans := arctan(y/x)/rad;
if x < 0 then
if y < 0 then ans := ans - 180.0
else ans := ans + 180.0;
end;
atan2 := ans;
end;
function vdot (u,v: vectorp): scalar;
begin
vdot := u↑.val[1]*v↑.val[1] + u↑.val[2]*v↑.val[2] + u↑.val[3]*v↑.val[3];
if u↑.refcnt <= 0 then relVector(u);
if v↑.refcnt <= 0 then relVector(v);
end;
function vmagn (v: vectorp): scalar;
begin
with v↑ do vmagn := sqrt(val[1]*val[1] + val[2]*val[2] + val[3]*val[3]);
if v↑.refcnt <= 0 then relVector(v);
end;
(* vector functions: vmake, svmul, vsdiv, vadd, vsub, unitv, crossv, tvmul *)
function vmake (a,b,c: scalar): vectorp;
var v: vectorp;
begin
v := newVector;
with v↑ do begin val[1] := a; val[2] := b; val[3] := c; end;
vmake := v;
end;
function svmul (s: scalar; v: vectorp): vectorp;
var u: vectorp; i: 1..3;
begin
if v↑.refcnt <= 0 then u := v else u := newVector;
with v↑ do for i:= 1 to 3 do u↑.val[i] := s * val[i];
svmul := u;
end;
function vsdiv (v: vectorp; s: scalar): vectorp;
var u: vectorp; i: 1..3;
begin
if v↑.refcnt <= 0 then u := v else u := newVector;
if s = 0 then
begin
pp20L('vsdiv: attempt to di',20); pp20('vide by zero! ',13); ppLine;
end;
with v↑ do for i:= 1 to 3 do u↑.val[i] := val[i] / s;
vsdiv := u;
end;
function vadd (u,v: vectorp): vectorp;
var w: vectorp; i: 1..3;
begin
w := newVector;
with w↑ do for i:= 1 to 3 do val[i] := u↑.val[i] + v↑.val[i];
if u↑.refcnt <= 0 then relVector(u);
if v↑.refcnt <= 0 then relVector(v);
vadd := w;
end;
function vsub (u,v: vectorp): vectorp;
var w: vectorp; i: 1..3;
begin
w := newVector;
with w↑ do for i:= 1 to 3 do val[i] := u↑.val[i] - v↑.val[i];
if u↑.refcnt <= 0 then relVector(u);
if v↑.refcnt <= 0 then relVector(v);
vsub := w;
end;
function unitv (v: vectorp): vectorp;
begin
unitv := vsdiv(v,vmagn(v));
end;
function vcross (u,v: vectorp): vectorp;
var w: vectorp; i: 1..3;
begin
w := newVector;
with w↑ do
begin
val[1] := u↑.val[2]*v↑.val[3] - u↑.val[3]*v↑.val[2];
val[2] := u↑.val[3]*v↑.val[1] - u↑.val[1]*v↑.val[3];
val[3] := u↑.val[1]*v↑.val[2] - u↑.val[2]*v↑.val[1];
end;
if u↑.refcnt <= 0 then relVector(u);
if v↑.refcnt <= 0 then relVector(v);
vcross := w;
end;
function tvmul (t: transp; v: vectorp): vectorp;
var u: vectorp; i,j: 1..3;
begin
u := newVector;
with u↑ do
for i:= 1 to 3 do
begin
val[i] := t↑.val[i,4];
for j := 1 to 3 do val[i] := val[i] + t↑.val[i,j] * v↑.val[j];
end;
if t↑.refcnt <= 0 then relTrans(t);
if v↑.refcnt <= 0 then relVector(v);
tvmul := u;
end;
(* trans extraction routines: tpos, torient, taxis, tmagn *)
function tpos (t: transp): vectorp;
var v: vectorp; i: 1..3;
begin
v := newVector;
with t↑ do for i:= 1 to 3 do v↑.val[i] := val[i,4];
if t↑.refcnt <= 0 then relTrans(t);
tpos := v;
end;
function torient (t: transp): transp;
var r: transp; i,j: 1..3;
begin
r := newTrans;
with t↑ do
for i := 1 to 3 do
begin
for j := 1 to 3 do r↑.val[i,j] := val[i,j];
r↑.val[i,4] := 0;
end;
if t↑.refcnt <= 0 then relTrans(t);
torient := r;
end;
function ssqrt(v: real): real;
begin if (-0.000001 < v) and (v < 0) then ssqrt:= 0.0 else ssqrt:= sqrt(v) end;
function taxis (t: transp): vectorp;
(* extracts the axis of rotation from a trans *)
var cx,cy,cz,a,b,c,d,cw: real;
begin
a := t↑.val[1,1];
b := t↑.val[2,2];
c := t↑.val[3,3];
cw := (a+b+c-1)/2;
if cw > 0.9999 then taxis := vmake(0,0,1) (* vector for nilrot = zhat *)
else (* *** use zhat ↑↑ in OMSI PASCAL version *** *)
with t↑ do
begin
d := 3-a-b-c;
cz := ssqrt((-a-b+c+1)/d);
if cz < 0.001 then cy := ssqrt((-a+b-c+1)/d)
else begin cy := (val[3,2] - val[1,2]*val[3,1] / (a-1)) * cz ;
cy := cy / (1 - b + val[1,2]*val[2,1] / (a-1)); end;
if cz+abs(cy) < 0.001 then cx := ssqrt((a-b-c+1)/d)
else cx := -(val[2,1]*cy + val[3,1]*cz) / (a-1);
taxis := vmake(cx,cy,cz);
end;
if t↑.refcnt <= 0 then relTrans(t);
end;
function tmagn (t: transp): scalar;
(* finds the angle of rotation in a trans *)
var cx,cy,cz,a,b,c,d,cw,s: real;
begin
with t↑ do
begin
a := val[1,1];
b := val[2,2];
c := val[3,3];
cw := (a+b+c-1)/2;
if cw > 0.9999 then tmagn := 0 (* angle for nilrot *)
else
begin
d := 3-a-b-c;
cz := ssqrt((-a-b+c+1)/d);
if cz < 0.001 then cy := ssqrt((-a+b-c+1)/d)
else begin cy := (val[3,2] - val[1,2]*val[3,1] / (a-1)) * cz ;
cy := cy / (1 - b + val[1,2]*val[2,1] / (a-1)); end;
if cz+abs(cy) < 0.001 then cx := ssqrt((a-b-c+1)/d)
else cx := -(val[2,1]*cy + val[3,1]*cz) / (a-1);
if (-1.000001 < cw) and (cw < -1.0) then s := 180
else if (1.0000 < cw) and (cw < 1.000001) then s := 0
else s := acos(cw);
if abs(cz) >= 0.577 then
begin if (val[1,2]-val[2,1])/cz > 0 then s := -s end
else if abs(cy) >= 0.577 then
begin if (val[3,1]-val[1,3])/cy > 0 then s := -s end
else if abs(cx) >= 0.577 then
begin if (val[2,3]-val[3,2])/cx > 0 then s := -s end
else
begin
pp20L('tmagn: rotation stra',20); pp10('ngeness! ',8); ppLine;
end;
tmagn := s;
end;
if refcnt <= 0 then relTrans(t);
end;
end;
(* trans functions: tmake, tvadd, tvsub, ttmul, tinvrt, vsaxwr, constr, vmkfrc *)
procedure tcopy(var tp,t: transval); (* auxiliary routine *)
var i,j: 1..4;
begin for i := 1 to 3 do for j := 1 to 4 do tp[i,j] := t[i,j]; end;
function tmake (t: transp; v: vectorp): transp;
var tp: transp; i: 1..3;
begin
if t↑.refcnt <= 0 then tp := t
else begin tp := newTrans; tcopy(tp↑.val,t↑.val); end; (* copy rotation part *)
with tp↑ do
for i := 1 to 3 do val[i,4] := v↑.val[i]; (* and vector part *)
if v↑.refcnt <= 0 then relVector(v);
tmake := tp;
end;
function tvadd (t: transp; v: vectorp): transp;
var tp: transp; i,j: 1..3;
begin
if t↑.refcnt <= 0 then tp := t
else begin tp := newTrans; tcopy(tp↑.val,t↑.val); end; (* copy rotation part *)
with tp↑ do
for i := 1 to 3 do val[i,4] := val[i,4] + v↑.val[i]; (* add in vector *)
if v↑.refcnt <= 0 then relVector(v);
tvadd := tp;
end;
function tvsub (t: transp; v: vectorp): transp;
var tp: transp; i,j: 1..3;
begin
if t↑.refcnt <= 0 then tp := t
else begin tp := newTrans; tcopy(tp↑.val,t↑.val); end; (* copy rotation part *)
with tp↑ do
for i := 1 to 3 do val[i,4] := val[i,4] - v↑.val[i]; (* subtract vector *)
if v↑.refcnt <= 0 then relVector(v);
tvsub := tp;
end;
function ttmul (t1,t2: transp): transp;
var tp: transp; i,j,k: 1..4;
begin
tp := newTrans;
with tp↑ do
for i := 1 to 3 do
begin
for j := 1 to 4 do (* rotate t2 by orient(t1) *)
begin
val[i,j] := 0;
for k := 1 to 3 do val[i,j] := val[i,j] + t1↑.val[i,k]*t2↑.val[k,j];
end;
val[i,4] := val[i,4] + t1↑.val[i,4]; (* add in t1 vector offset *)
end;
if t1↑.refcnt <= 0 then relTrans(t1);
if t2↑.refcnt <= 0 then relTrans(t2);
ttmul := tp;
end;
function tinvrt (t: transp): transp;
var tp: transp; i,j,k: 1..4;
begin (* The result, (rot',trslat'), is defined: rot' = transpose(rot)
trslat' = -(rot'*trslat) *)
tp := newTrans;
with tp↑ do
for i := 1 to 3 do
begin
val[i,4] := 0;
for j := 1 to 3 do
begin
val[i,j] := t↑.val[j,i]; (* transpose rotation part *)
val[i,4] := val[i,4] - t↑.val[j,i] * t↑.val[j,4]; (* build up new vector offset *)
end;
end;
if t↑.refcnt <= 0 then relTrans(t);
tinvrt := tp;
end;
function vsaxwr(ax: vectorp; w: real): transp;
var cx,cy,cz,sw,cw: real; r: transp; i: 1..3;
begin (* produces a rotn, given axis vector ax & magnitude w *)
ax := unitv(ax);
cx := ax↑.val[1];
cy := ax↑.val[2];
cz := ax↑.val[3];
sw := sind(w); cw := cosd(w);
r := newTrans;
with r↑ do
begin
val[1,1] := cw + (1-cw) * cx * cx;
val[1,2] := (1-cw) * cx * cy - cz * sw;
val[1,3] := (1-cw) * cx * cz + cy * sw;
val[2,1] := (1-cw) * cx * cy + cz * sw;
val[2,2] := cw + (1-cw) * cy * cy;
val[2,3] := (1-cw) * cy * cz - cx * sw;
val[3,1] := (1-cw) * cx * cz - cy * sw;
val[3,2] := (1-cw) * cy * cz + cx * sw;
val[3,3] := cw + (1-cw) * cz * cz;
for i := 1 to 3 do val[i,4] := 0;
end;
if ax↑.refcnt <= 0 then relVector(ax);
vsaxwr := r;
end;
function construct(org,vx,vxy: vectorp): transp;
var t: transp; x,y,z: vectorp; i: 1..3;
begin
org↑.refcnt := org↑.refcnt + 1; (* so we don't release org too soon *)
x := unitv(vsub(vx,org)); (* ok to reclaim vx now *)
vxy := vsub(vxy,org); (* ok to reclaim vxy now *)
z := unitv(vcross(x,vxy));
y := vcross(z,x);
t := newTrans;
with t↑ do
for i := 1 to 3 do
begin
val[i,1] := x↑.val[i];
val[i,2] := y↑.val[i];
val[i,3] := z↑.val[i];
val[i,4] := org↑.val[i];
end;
org↑.refcnt := org↑.refcnt - 1;
if org↑.refcnt <= 0 then relVector(org); (* can reclaim org now *)
relVector(x); relVector(y); relVector(z); (* done with these too *)
construct := t;
end;
function vmkfrc(v: vectorp): transp;
var t: transp; x,y,z,a,b: real; i: integer;
begin (* takes force vector and makes a trans with the x-axis along vector *)
v := unitv(v); (* make force vec into unit vector *)
with v↑ do
begin x := val[1]; y := val[2]; z := val[3]; end;
relVector(v); (* done with v now *)
t := newTrans;
with t↑ do
begin
val[1,1] := x;
val[1,2] := y;
val[1,3] := z;
if (x = 0) and (y = 0) then
begin
for i := 1 to 3 do begin val[2,i] := 0; val[3,i] := 0 end;
val[2,2] := z;
val[3,1] := -z;
end
else
begin
b := sqrt(x*x + y*y);
a := - y / b;
b := x / b;
val[2,1] := a;
val[2,2] := b;
val[2,3] := 0;
val[3,1] := - b * z;
val[3,2] := a * z;
val[3,3] := b * x - a * y;
end;
for i := 1 to 3 do val[i,4] := 0;
end;
vmkfrc := t;
end;
begin end.